package yxy.game.pm2.active.command.weeklycard;

import yxy.apple.logger.Logger;
import yxy.cherry.data.bean.actweekcarkgift;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_周卡;
import yxy.game.pm2.active.module.actdata.ActDataCenter;
import yxy.game.pm2.active.module.subordinateclass.weeklycard.WeeklyWish;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;

import java.util.Map;

/**
 * 周卡_许愿信息
 * @author Dell
 *
 */
@MessageType(proto = Command.ActWeeklyCardWishInfo, classtype = ActiveMessage.ActWeeklyCardWishInfoRequest.class)
public class CMD_Active_周卡_许愿_Info extends Command<ActiveMessage.ActWeeklyCardWishInfoRequest> {

	private static final Logger logger = Logger.create(CMD_Active_周卡_许愿_Info.class);
	
	@Override
	public void execute(MessageSource messageSource, ActiveMessage.ActWeeklyCardWishInfoRequest request) throws Exception {
		String playercode = messageSource.getTag();
		ActiveMessage.ActWeeklyCardWishInfoResponse.Builder response = ActiveMessage.ActWeeklyCardWishInfoResponse.newBuilder();
		
		ActiveModule_周卡 am = ActiveModule_周卡.get(playercode,false);
		if(am == null) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}
		
		if(!am.running()) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}

		WeeklyWish ww = am.getWw();
		Map<String, actweekcarkgift> awcgmap = ActDataCenter.getAllData(actweekcarkgift.class, playercode);
		for(int id : ww.getIds()) {
			ActiveMessage.WeeklyCardWish.Builder build = ActiveMessage.WeeklyCardWish.newBuilder();
			build.setId(id);
			
			boolean unlock = ww.getUnlockids().contains(id);
			build.setUnlock(unlock);
			
			boolean select = ww.getSelectids().contains(id);
			build.setSelect(select);
			
			actweekcarkgift data = awcgmap.get(String.valueOf(id));
			build.setCondition1(data.getCondition1());
			build.setCondition2(data.getCondition2());
			
			Currency cur = Currency.create(data.getRewardtype(), data.getRewardid(), data.getRewardnum());
			PackageMessage.Item item = new CurrencyHandle().packItem(cur);
			build.addCurs(item);
			
			response.addWcws(build.build());
		}

		response.setEnd(am.getTimeline().getEndTime().getMillis());
		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, ActiveMessage.ActWeeklyCardWishInfoResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}
